<?php

class CommunityAction extends Action{
    public function Mypage(){   
            //普通方式实现分页   
        $COMMENT = M('random_answer_record');
		import('@.ORG.Page');// 导入分页类
		$count      = $COMMENT->count();// 查询满足要求的总记录数
        $p = new Page ( $count,5);    //每一页放5条数据 
        $list=$COMMENT->limit($p->firstRow.','.$p->listRows)->order('id')->select();
        
        $page=$p->show();   
        $this->assign ( "page", $page );   
        $this->assign ( "list", $list );   
        // p($page);
        var_dump($page);
        p($list);
    }   

	public function index(){
		$openId = session('?openId')? : $this->error('由于某种原因，您的某些关键数据丢失，请在微信端发送000000重新授权登录,然后再获取该链接');
		$COMMENT = D('Student/CommunityComment');
		$REPLY = D('Student/CommunityReply');
		$commentNum = $COMMENT->count();
		$replyNum = $REPLY->count();
		$sumNum = $commentNum + $replyNum;
		import('@.ORG.Page');// 导入分页类
		$p = new Page ( $commentNum,5);    //每一页放5条数据 
		$community=$COMMENT->limit($p->firstRow.','.$p->listRows)->order('time desc')->select();
        $page=$p->show();   
        $this->assign ( "page", $page );   
		$this->assign('sumNum',$sumNum);
		$this->assign('commentNum',$commentNum);
		$this->assign('community',$community);
		$this->display('index');
		
	}
	public function comment(){
		
		$this->display();
	}
	public function handleComment(){
		if(!IS_AJAX)
			$this->error('你请求的页面不存在，请重新获得该页面');
		$openId = session('?openId')? : $this->error('由于某种原因，您的某些关键数据丢失，请在微信端发送000000重新授权登录,然后再获取该链接');
		$comment = I('comment');
		$COMMENT = D('Student/CommunityComment');
		$STU = D('Student/StudentInfo');
		$name = $STU->getName($openId);
		$class = $STU->getClass($openId);
		$community = array(
				'openId'  => $openId,
				'name'    => $name,
				'class'   => $class,
				'comment' => $comment,
				'time'    => date("Y-m-d H:i:s"));
		if($COMMENT->data($community)->add())
			$this->ajaxReturn(array('status' => 'success'),'json');
		else
			$this->ajaxReturn(array('status' => 'faliure'),'json');
	}
	public function reply(){
		$commentId = I('commentId');
		$this->assign('commentId',$commentId);
		$this->display();
	}
	public function handleReply(){
		$openId = session('?openId')? : $this->error('由于某种原因，您的某些关键数据丢失，请在微信端发送000000重新授权登录,然后再获取该链接');
		$replyContent = I('reply');
		$commentId = I('commentId');
		$STU = D('Student/StudentInfo');
		$REPLY = D('Student/CommunityReply');
		$name = $STU->getName($openId);
		$class = $STU->getClass($openId);

		$reply = array(
				'openId'       => $openId,
				'name'         => $name,
				'class'        => $class,
				'commentId'    => $commentId,
				'replyContent' => $replyContent,
				'time'         => date("Y-m-d H:i:s"));

		if($REPLY->data($reply)->add())
			$this->ajaxReturn(array('status' => 'success'),'json');
		else
			$this->ajaxReturn(array('status' => 'faliure'),'json');

	}
}
